<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . 
		'/SAGER/model/interfaces/IClientAddressAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IConnectionAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/ConnectionFactory.php';
	
	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];

	class  MySqlClientAddressAccessDao implements IClientAddressAccessDao {
 		/**
		 * Constructor of the client address dao
		 */
	 	public function __construct () {}
	
		/**
		 * Creates a client address
		 */
		public function createClientAddress($clientAddress) {
			$startDate = $clientAddress -> getStartDate();
			$endDate = $clientAddress -> getEndDate();
			$idClient = $clientAddress -> getIdClient();
			$ciRif = $clientAddress -> getCiRif();
			$idPlace = $clientAddress -> getIdPlace();
			
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			if (strcmp($ciRif, 'ci') == 0) {
				if ($idPlace)
					$query = "INSERT INTO CLIENT_ADDRESS (start_date, 
						fk_id_number_natural_customer, fk_id_place) 
						VALUES ('$startDate', $idClient, $idPlace)";
				
				else
					$query = "INSERT INTO CLIENT_ADDRESS (start_date, 
						fk_id_number_natural_customer, fk_id_place) 
						VALUES ('$startDate', $idClient, " . mysql_insert_id() . ")";
			}
			
			else {
				if ($idPlace)
					$query = "INSERT INTO CLIENT_ADDRESS (start_date, 
						fk_rif_legal_client, fk_id_place) 
						VALUES ('$startDate', '$idClient', $idPlace)";
				
				else
					$query = "INSERT INTO CLIENT_ADDRESS (start_date, 
						fk_rif_legal_client, fk_id_place) 
						VALUES ('$startDate', '$idClient', " . mysql_insert_id() . ")";
			}
					
			$sentence = mysql_query($query, $conn);
			
			if ($sentence)
				return true;
			
			return false;
		}
		
		/**
		 * Selects a client address by natural customer id
		 */
		public function selectClientAddressByNaturalCustomerIdNumber($ci) {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "SELECT CONCAT(zone.name, ', ', city.name, ', ', state.name, ', ',
						  country.name) address
					  FROM CLIENT_ADDRESS ca, PLACE country, PLACE state, PLACE city, PLACE zone
					  WHERE ca.fk_id_number_natural_customer = $ci
					  AND ca.fk_id_place = zone.pk_id
					  AND zone.fk_id_place = city.pk_id
					  AND city.fk_id_place = state.pk_id
					  AND state.fk_id_place = country.pk_id";
					  
			$sentence = mysql_query($query, $conn);
			
			$conn = $connection -> closeConnection($conn);
			
			return $sentence;
		}
		
		/**
		 * Selects a client address by legal client rif
		 */
		public function selectClientAddressByLegalClientRif($rif) {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "SELECT CONCAT(zone.name, ', ', city.name, ', ', state.name, ', ',
						  country.name) address
					  FROM CLIENT_ADDRESS ca, PLACE country, PLACE state, PLACE city, PLACE zone
					  WHERE ca.fk_rif_legal_client = '$rif'
					  AND ca.fk_id_place = zone.pk_id
					  AND zone.fk_id_place = city.pk_id
					  AND city.fk_id_place = state.pk_id
					  AND state.fk_id_place = country.pk_id";
					  
			$sentence = mysql_query($query, $conn);
			
			$conn = $connection -> closeConnection($conn);
			
			return $sentence;
		}
	}
?>